package com.example.demo.controller;

import com.example.demo.AppUtil.BaseActionUtil;
import com.example.demo.entity.AfoneCategory;
import com.example.demo.service.IAfoneCategoryService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
 * @Author:大力王
 * @Date:2024/6/26-26-7:12
 * 开始了
 */
@Api(tags = "分类接口")
@RestController
@RequestMapping("/catalog")
public class AfCatalogController extends BaseActionUtil {
    @Autowired
    private IAfoneCategoryService afoneCategoryService;
    /**
     * 获取分类栏目数据
     */
    @ApiOperation(value = "获取分类栏目数据")
    @RequestMapping("/index")
    @ApiOperationSupport(author = "Mr.力铉器 Croat")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id", paramType = "query", required = false),
            @ApiImplicitParam(name = "page", value = "page", paramType = "query", required = false),
            @ApiImplicitParam(name = "size", value = "size", paramType = "query", required = false)})
    public Object index(
            Integer id,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "size", defaultValue = "10") Integer size
    ) {
        Map<String, Object> resultObj = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("page", page);
        params.put("limit", size);
        params.put("sidx", "sort_order");
        params.put("order", "asc");
        params.put("parentId", 0);
        //查询列表数据
        List<AfoneCategory> data = afoneCategoryService.queryList(params);

        AfoneCategory currentCategory = null;
        if (null != id) {
            currentCategory = afoneCategoryService.queryObject(id);
        }
        if (null == currentCategory && null != data && data.size() != 0) {
            currentCategory = data.get(0);
        } else {
            currentCategory = new AfoneCategory();
        }

        //获取子分类数据
        if (null != currentCategory && null != currentCategory.getId()) {
            params.put("parentId", currentCategory.getId());
            currentCategory.setSubCategoryList(afoneCategoryService.queryList(params));
        }

        resultObj.put("categoryList", data);
        resultObj.put("currentCategory", currentCategory);
        return toResponseSuccess(resultObj);
    }
}
